package com.hr.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hr.domain.query.UserHousePageParams;
import com.hr.domain.query.UserHousePageResult;
import com.hr.domain.vo.UserHouseDetails;
import com.hr.domain.vo.UserHouseDisplayInPage;
import com.hr.mapper.FavoriteMapper;
import com.hr.mapper.UserHouseMapper;
import com.hr.service.FavoriteService;
import com.hr.service.RedisHouseService;
import com.hr.service.UserHouseService;
import com.hr.utils.UserContext;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserHouseServiceImpl implements UserHouseService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    @Lazy
    private RedisHouseService redisHouseService;

    @Autowired
    private UserHouseMapper  userHouseMapper;
    /**
     * 判断用户是否收藏了该房源
     * @param userId 用户ID
     * @param houseId 房源ID
     * @return 是否收藏
     */
    @Override
    public Boolean isUserCollectedHouse(Long userId, Long houseId) {
         if (userId == null || houseId == null){
             return false;
         }
       return favoriteMapper.isUserCollectedHouse(userId, houseId);
    }

    /**
     * 用户获取房源详情
     * @param houseId 房源ID
     * @param userId 用户ID
     * @return 房源详情
     */
    @Override
    public UserHouseDetails getUserHouseDetails(Long houseId, Long userId) {
        //1.先从缓存中获取数据
        UserHouseDetails houseDetailsFromCache = redisHouseService.getHouseDetailsFromCache(houseId, userId);

        //2.缓存中没有数据，从数据库中获取数据，再保存到缓存中
        if (houseDetailsFromCache == null){
            houseDetailsFromCache = redisHouseService.buildHouseDetails(houseId, userId);
            if (houseDetailsFromCache != null){
                // 3. 如果是热门房源，缓存时间长一些
                long expireTime = redisHouseService.isHotHouse(houseId) ?
                        86400 : 7200; // 热门房源缓存24小时，普通房源2小时
                redisHouseService.saveHouseDetailsToCache(houseDetailsFromCache, expireTime);
            }
        }
        // 3. 记录点击量
        redisHouseService.clickHouseDetailsViews(houseId);

        return houseDetailsFromCache;

    }

    /**
     * 获取热门房源列表
     * @param count 获取数量
     * @return 热门房源列表
     */
    @Override
    public List<UserHouseDetails> getHotHouses(int count) {
        // 获取热门房源ID列表
        List<Long> hotHouseIds = redisHouseService.getHotHouseIds(count);
        if (hotHouseIds == null || hotHouseIds.isEmpty()) {
            return new ArrayList<>();
        }
        // 获取当前登录用户ID
        Long userId = UserContext.getUserId();
        // 批量获取热门房源详情
        List<UserHouseDetails> hotHouses = new ArrayList<>();
        for (Long houseId : hotHouseIds) {
            UserHouseDetails details = redisHouseService.getHouseDetailsFromCache(houseId, userId);
            if (details == null) {
                details = redisHouseService.buildHouseDetails(houseId, userId);
                if (details != null) {
                    redisHouseService.saveHouseDetailsToCache(details, 43200);//设置的时间为12小时
                }
            }
            if (details != null) {
                hotHouses.add(details);
            }
        }
        return hotHouses;
    }

    /**
     * 分页获取用户视角的房源列表
     * @param userHousePageParams 分页参数
     * @return 房源列表
     */
    @Override
    public UserHousePageResult getUserHouseList(UserHousePageParams userHousePageParams) {
        //开始分页
        PageHelper.startPage(userHousePageParams.getPageNum(), userHousePageParams.getPageSize());
        //传递分页参数
        List<UserHouseDisplayInPage> houseList = userHouseMapper.getUserHouseList(userHousePageParams);
        //强转返回结果为Page<UserHouseDetails>
        Page<UserHouseDisplayInPage> pageData = (Page<UserHouseDisplayInPage>) houseList;
        //返回数据
        return new UserHousePageResult(pageData.getTotal(), pageData.getResult());
    }
}
